package servlet;

import entity.MetaverseProduct;
import service.MetaverseProductService;
import service.impl.MetaverseProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/MetaverseProductAdd")
public class MetaverseProductAdd extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("test/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        MetaverseProduct metaverseProduct = new MetaverseProduct();
        metaverseProduct.setProduct_name(req.getParameter("name"));
        metaverseProduct.setProduct_desc(req.getParameter("desc"));
        metaverseProduct.setProduct_price(Double.parseDouble(req.getParameter("price")));
        metaverseProduct.setFlag(Integer.parseInt(req.getParameter("flag")));

        MetaverseProductService metaverseProductService = new MetaverseProductServiceImpl();
        if(metaverseProductService.metaverseAdd(metaverseProduct)>0){
            //成功
            out.print("<script>alert('添加成功');</script>");
        }else{
            //失败
            out.print("<script>alert('添加成功');</script>");
        }

        resp.sendRedirect("MetaverseProductList");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
